package sa4
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletRequest
import scala.xml.PrettyPrinter
import javax.servlet.http.HttpServlet

class App extends HttpServlet {
  
  val framework = new MainFramework
  
  override def service(req: HttpServletRequest, resp: HttpServletResponse) {
    //current context (You don`t say?!)
    framework.currentContext() = new Context(req, resp)
    if (framework.tableDispatch(req, resp))
      return
    resp.sendError(HttpServletResponse.SC_NOT_FOUND)
  }
  
  framework.addRoute("/", ciao)
  
  def ciao(req: HttpServletRequest, resp: HttpServletResponse) {
    val xml = framework.renderPageWelcome
    val html = framework.prepareForm(xml)
    resp.getWriter.write(new PrettyPrinter(72, 2).formatNodes(html))
  }

}